(ns eventsmashup.data
  (:use [net.cgrand.xml :as en-xml])
  (:require [clojure.xml :as xml]
            [clojure.zip :as zip]
            [clojure.contrib.zip-filter.xml :as zf]
            [eventsmashup.apicalls]
            [clojure.java.io]))
 
(def *dummy-content*
   {:title "Events Mashup"
      :rows [
             {        :event-data [{:event-name "event name 1"
                           :performer "performer 1"
                          :date "date 1"
                           :start-time "start time 1"
                           :end-time "start time 1"}]}
      {
                  :event-data [{:event-name "event name 2"
                           :performer "performer 2"
                          :date "date 2"
                           :start-time "start time 2"
                           :end-time "start time 2"}]}]})
 
 ;(def eventful-events "http://api.eventful.com/rest/events/search?app_key=4H4Vff4PdrTGp3vV&keywords=music&location=Belgrade&date=Future")

(defn get-feed [feed]
  (en-xml/xml-zip (feed)))
  ;(xml/parse "events-search.xml"))
;(deftemplate foo (xml-resource "foo.xml") [] [:xyz:foo] (content "test2"))
;
(defn get-value [feed tags &more]
  (apply zf/xml-> (zip/xml-zip (get-feed[feed])) (conj (vector tags &more) zf/text)))

(defstruct event :id :url :event-name :duration :venue :allday :recur-instances :performers :image :categories)

(defstruct duration :start-time :stop-time)
(defstruct venue :id :name :location)
(defstruct location :lat :long :name)
(defstruct recurence :description :recur-instances)
(defstruct recur-instances :id :recurence :all-day)
(defstruct performer :id)
(defstruct image :url :width :height :thumb)
(defstruct category :id)
(defstruct artist :mbid :name :gender :country :life-span :tag-list :image :summary)
(defstruct lifespan :begin :end)
(defstruct tag-list :tag :name :url)

;parsing the xml file
(defn parsing-xml [xml-data](for [x (xml-seq (xml/parse xml-data))
        :when (= :title (:tag x))]
    (:content x)))

(defn zip-str [feed] 
  (zip/xml-zip (get-feed[feed]))) 

(defn get-appropriate-value [feed-name search-value])
  
(defn get-events [events]
"Extract specific elements from an events in eventful"
; (filter pred coll)
;
  {:event (struct-map event
             :id (zf/xml-> events :id  zf/text) 
             :url (zf/xml-> events :url)
             :title (zf/xml-> events :title)
             :description (zf/xml-> events :description)
             :duration (struct-map duration :start-time (zf/xml-> events :duration :start-time)
                                              :stop-time (zf/xml-> events :duration :stop-time))
             :venue (zf/xml-> events :venue)
             :allday (zf/xml-> events :allday)
             :recur-instances (zf/xml-> events :recur-instances)
             :date-created (zf/xml-> events :date-created)
             :owner (zf/xml-> events :owner)
             :privacy (zf/xml-> events :privacy)
             :modified(zf/xml-> events :modified)
             :performers (zf/xml-> events :performers)
             :image (zf/xml-> events :image)
             :categories (zf/xml-> events :categories)
             :links(zf/xml-> events :links))})

(defn get-artist-last-fm [artist-last-fm artist-musicbrainz]
  (struct-map artist 
                        :mbid (zf/xml-> artist-last-fm :mbid)
                        :name  (zf/xml-> artist-last-fm :name)
                        :gender (zf/xml-> artist-musicbrainz :gender)
                        :country (zf/xml-> artist-musicbrainz :name)
                        :life-span  (struct-map lifespan :begin (zf/xml-> artist-musicbrainz :lifespan :begin)
                                              :end (zf/xml-> artist-musicbrainz :lifespan :end))
                        :tag-list (struct-map tag-list :begin (zf/xml-> artist-musicbrainz :tag-list :tag)
                                              :url (zf/xml-> artist-last-fm :tag-list :url))
                        :image (zf/xml-> artist-last-fm :image-large)
                        :summary (zf/xml-> artist-last-fm :summary)))